import Toast from '../vant/weapp/toast/toast';

const http = (url, param, method, callback) => {
  // param.admin_id = wx.getStorageSync('admin_id')
  wx.request({
    url: url,
    header: {
      token: wx.getStorageSync('token'),
      appid: wx.getAccountInfoSync().miniProgram.appId,
      header: {
        'content-type': 'application/x-wwww-form-urlencoded;charset=utf-8',
      },
    },
    data: param,
    method: method,
    success: (res) => {
      if (res.data.code == 200 || res.data.code == 2000 || res.data.code == 0 || res.data.code == 1002) {
        callback(res.data);
        // console.log(res, 99)
        Toast.clear()
      } else {
        if (res.data.code == 401) {
          wx.setStorageSync('token', '')
          wx.setStorageSync('mobile', '')
          // Toast({
          //     type: 'fail',
          //     message: '请先登录！',
          //     forbidClick: true,
          //     onClose: () => {
          //         // wx.reLaunch({
          //         //     url: '/pages/person/index',
          //         // })

          //     },
          // });
          callback(res.data);
          return;

        }
        if (url == "https://zly.qile168.net/api/order/delete") {
          //防止弹窗
          Toast.clear()
          callback(res.data);
          return
        }
        Toast({
          type: 'fail',
          forbidClick: true,
          message: res.data.message,
        });
        callback(res.data);
      }
    },
    fail: (res) => {
      console.log(res)
      Toast({
        type: 'fail',
        message: '系统忙不过来了,请稍后重试',
      });
      callback(res);
    }
  });
}

export function get(url, param, callback) {
  return http(url, param, 'get', callback);
}

export function post(url, param, callback) {
  return http(url, param, 'post', callback);
}

const wecatpay = (order_on, type, callback) => {
  wx.login({
    success(codes) {
      if (codes.code) {
        post("https://zly.qile168.net/api/Wecat/index", {
          order_on: order_on,
          code: codes.code,
          type: type
        }, (re) => {
          console.log(re)
          if (re.code == 200) {
            wx.requestPayment({
              appid: re.data.parameter.appId,
              timeStamp: re.data.parameter.timeStamp,
              nonceStr: re.data.parameter.nonceStr,
              package: re.data.parameter.package,
              signType: re.data.parameter.signType,
              paySign: re.data.parameter.paySign,
              success(da) {
                if (da.errMsg == 'requestPayment:ok') {
                  Toast({
                    type: 'success',
                    message: '购买成功',
                    onClose: () => {

                    },
                  });

                }
                callback(da);
              },
              fail() {
                Toast({
                  type: 'fail',
                  message: re.msg,
                  onClose: () => {

                  },
                });
                callback();
              }
            })
          } else {
            Toast({
              type: 'fail',
              message: re.msg,
              onClose: () => {

              },
            });
          }


        })

      }
    }
  })

}
const wecatpayfz = (order_on, type, callback) => {
  wx.login({
    success(codes) {
      if (codes.code) {
        post("https://zly.qile168.net/api/Wecat/indexv1", {
          order_on: order_on,
          code: codes.code,
          type: type
        }, (re) => {
          console.log(re)
          wx.requestPayment({
            appid: re.data.appId,
            timeStamp: re.data.timeStamp,
            nonceStr: re.data.nonceStr,
            package: re.data.package,
            signType: re.data.signType,
            paySign: re.data.paySign,
            success(da) {
              console.log(da)
              if (da.errMsg == 'requestPayment:ok') {

                Toast({
                  type: 'success',
                  message: '购买成功',
                  onClose: () => {

                  },
                });

              }

              callback(da);
            },
            fail() {
              console.log('fks')
              Toast({
                type: 'fail',
                message: '购买失败',
                onClose: () => {

                },
              });
              callback();
            }
          })
        })

      }
    }
  })

}
export function wecat(order_on, type, callback) {
  return wecatpay(order_on, type, callback);
}

export function wecatfz(order_on, type, callback) {
  return wecatpayfz(order_on, type, callback);
}